<?php

/**
 * This file holds the library functions for table management.
 * 
 * @package Table_library
 */


require_once("lib/db.lib.php");
require_once("conf/game.conf.php");


/**
 * Initialize the tables.
 * Delete all tables and add back empty tables.
 * 
 * @param	int	$num_of_tables	Number of tables to create.(Default: GAME_DEFAULT_NUM_OF_TABLE)
 */
function table_initialize($num_of_tables = GAME_DEFAULT_NUM_OF_TABLE){
	
	db_connect();
	
	// Truncate table;	
	mysql_query("LOCK TABLES table_info WRITE;");
	
	$sql = "TRUNCATE `table_info`;";
	$result = mysql_query($sql);	
	
	for($i=1;$i<=$num_of_tables;$i++){
		$sql = "INSERT INTO `table_info` (`table_id`) VALUES ($i);";
		mysql_query($sql);
	}
	
	mysql_query("UNLOCK TABLES;");
	
}

/**
 * Find the table that the user is sitting in.
 * 
 * @param	int		$user_id		User ID.
 * @param	bool	$no_escape		Indicate whether to escape the variables or not.
 * 
 * @return	bool	<b>Table ID</b> if successed, <b>FALSE</b> if failed.
 */
function table_get_user_table($user_id, $no_escape = FALSE){
	
	db_connect();
	
	if(!$no_escape){
		$user_id = mysql_real_escape_string($user_id);
	}
	
	
	$sql = "SELECT `table_id` FROM `table_info` WHERE `seat1` = \"$user_id\" OR `seat2` = \"$user_id\" OR `seat3` = \"$user_id\" OR `seat4` = \"$user_id\" OR `seat5` = \"$user_id\";";
	
	$result = mysql_query($sql);
		
	
	//return
	if($row = mysql_fetch_assoc($result)){
		return (int) $row['table_id'];
	}
		
	return FALSE;
	
}

/**
 * Find the seat number that the user is sitting on in a table.
 * 
 * @param	int		$table_id		Table ID.
 * @param	int		$user_id		User ID.
 * @param	bool	$no_escape		Indicate whether to escape the variables or not.
 * 
 * @return	bool	<b>Table ID</b> if successed, <b>FALSE</b> if failed.
 */
function table_get_user_table_seat($table_id, $user_id, $no_escape = FALSE){
	
	db_connect();
	
	if(!$no_escape){
		$table_id = mysql_real_escape_string($table_id);
		//$user_id = mysql_real_escape_string($user_id);
	}
	
	$sql = "SELECT `seat1`, `seat2`, `seat3`, `seat4`, `seat5` FROM `table_info` WHERE `table_id` = \"$table_id\" ;";
	
	$result = mysql_query($sql);
		
	
	//return
	if($row = mysql_fetch_assoc($result)){
		if($key = array_search($user_id, $row)){
			return (int) $key[strlen($key) - 1]; //get last char (which is the seat no.)
		}
	}
		
	return FALSE;
}


/**
 * Find the user that sat on a seat.
 * 
 * @param	int		$table_id		Table ID.
 * @param	int		$seat_number	Seat number.
 * @param	bool	$no_escape		Indicate whether to escape the variables or not.
 * 
 * @return	int|FALSE		<b>User ID</b> if successed, <b>zero</b> if seat is empty, <b>False</b> if failed.
 */
function table_get_table_seat_user($table_id, $seat_number, $no_escape = FALSE){

	db_connect();
	
	if(!$no_escape){
		$table_id = mysql_real_escape_string($table_id);
		$seat_number = mysql_real_escape_string($seat_number);
	}
	
	$seat = "seat".$seat_number;
	
	$sql = "SELECT `$seat` FROM `table_info` WHERE `table_id` = \"$table_id\";";
	
	$result = mysql_query($sql);
		
	
	//return
	if($result){
		$row = mysql_fetch_assoc($result);
		return (int) $row[$seat];
	}
		
	return FALSE;
}


/**
 * Seat a user in a table.
 * 
 * @param	int		$table_id		Table ID.
 * @param	int		$seat_number	Seat number. (Should be 1 <= x <= 5)
 * @param	int		$user_id		User ID.
 * @param	bool	$no_escape		Indicate whether to escape the variables or not.
 * 
 * @return	bool	<b>TRUE</b> if successed, <b>FALSE</b> if failed.
 */
function table_sit_down($table_id, $seat_number, $user_id, $no_escape = FALSE){
	
	db_connect();
	
	if(!$no_escape){
		$table_id = mysql_real_escape_string($table_id);
		$seat_number = mysql_real_escape_string($seat_number);
		$user_id = mysql_real_escape_string($user_id);
	}
	
	//user already sat down on other table.
	if(table_get_user_table($user_id)){
		return FALSE;
	}
	
	if(table_get_table_seat_user($table_id, $seat_number)){
		return FALSE;
	}
	
	$seat = "seat".$seat_number;
	
	$sql = "UPDATE `table_info` SET `$seat` = \"$user_id\" WHERE `table_id` = \"$table_id\";";
	
	
	//mysql_query("LOCK TABLES table_info WRITE;");
	
	$result = mysql_query($sql);	
	
	//mysql_query("UNLOCK TABLES;");
	
	if(mysql_affected_rows() > 0){
		return TRUE;
	}
		
	return FALSE;
	
}

/**
 * Empty a seat in a table.
 * 
 * @param	int		$table_id		Table ID.
 * @param	int		$seat_number	Seat number. (Should be 1 <= x <= 5)
 * @param	bool	$no_escape		Indicate whether to escape the variables or not.
 * 
 * @return	bool	<b>TRUE</b> if successed, <b>FALSE</b> if failed.
 */
function table_stand_up($table_id, $seat_number, $no_escape = FALSE){

	db_connect();
	
	if(!$no_escape){
		$table_id = mysql_real_escape_string($table_id);
		$seat_number = mysql_real_escape_string($seat_number);
	}
	
	$seat = "seat".$seat_number;
	
	$sql = "UPDATE `table_info` SET `$seat` = NULL WHERE `table_id` = \"$table_id\";";
	
	//mysql_query("LOCK TABLES table_info WRITE;");
	
	$result = mysql_query($sql);	
	
	//mysql_query("UNLOCK TABLES;");
	
	if(mysql_affected_rows() > 0){
		return TRUE;
	}
		
	return FALSE;
}


/**
 * Stand up a user.
 * 
 * @param	int		$user_id		User ID.
 * @param	bool	$no_escape		Indicate whether to escape the variables or not.
 * 
 * @return	bool	<b>TRUE</b> if successed, <b>FALSE</b> if failed.
 */
function table_user_stand_up($user_id, $no_escape = FALSE){
	
	$table_id = table_get_user_table($user_id, $no_escape);
	$seat_number = table_get_user_table_seat($table_id, $user_id, $no_escape);
	
	return table_stand_up($table_id, $seat_number, $no_escape);
}


/**
 * Get all users sitting in a table.
 * 
 * @param	int		$table_id		Table ID.
 * @param	bool	$no_escape		Indicate whether to escape the variables or not.
 * 
 * @return	Array	An array holding the user ID of all users.
 */
function table_get_users($table_id, $no_escape = FALSE){
	
	db_connect();
	
	if(!$no_escape){
		$table_id = mysql_real_escape_string($table_id);
	}
	
	$user_arr = array();
	for($i=1;$i<=5;$i++){
		$user = table_get_table_seat_user($table_id, $i);
		if($user){
			array_push($user_arr, $user);
		}
	}
	
	return $user_arr;
}

/**
 * Associate a game with a table.
 * 
 * @param	int		$table_id		Table ID.
 * @param	int		$game_id		Game ID.
 * 
 * @return	bool	<b>TRUE</b> if successed, <b>FALSE</b> if failed.
 */
function table_assoc_game($table_id, $game_id){
	
	db_connect();
	
	$sql = "UPDATE `table_info` SET `game_id` = \"$game_id\" WHERE `table_id` = \"$table_id\";";
	
	$result = mysql_query($sql);	
	
	if(mysql_affected_rows() > 0){
		return TRUE;
	}
		
	return FALSE;
}

/**
 * Remove a game in a table.
 * 
 * @param	int		$table_id		Table ID.
 * 
 * @return	bool	<b>TRUE</b> if successed, <b>FALSE</b> if failed.
 */
function table_remove_game($table_id){
	
	db_connect();
	
	$sql = "UPDATE `table_info` SET `game_id` = NULL WHERE `table_id` = \"$table_id\";";
	
	$result = mysql_query($sql);	
	
	if(mysql_affected_rows() > 0){
		return TRUE;
	}
		
	return FALSE;
}

/**
 * Find game id of a table.
 * 
 * @param	int		$table_id		Table ID.
 * 
 * @return	bool	<b>Table ID</b> if successed, <b>FALSE</b> if failed.
 */
function table_get_game_id($table_id){

	db_connect();
	
	$sql = "SELECT `game_id` FROM `table_info` WHERE `table_id` = \"$table_id\";";
	
	$result = mysql_query($sql);
	
	if($row = mysql_fetch_assoc($result)){
		return (int) $row['game_id'];
	}
		
	return FALSE;
	
}

